package com.dxx.cloud.admin.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dxx.cloud.admin.constant.CommonConst;
import com.dxx.cloud.admin.module.entity.OrdersEntity;
import com.dxx.cloud.admin.module.module.CustomerAddModule;
import com.dxx.cloud.admin.module.request.*;
import com.dxx.cloud.admin.module.response.OrdersResponse;
import com.dxx.cloud.admin.module.response.wx.CreateOrderResponse;
import com.dxx.cloud.admin.service.ICustomerService;
import com.dxx.cloud.admin.service.IOrdersService;
import com.dxx.cloud.admin.utils.EntityUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author duh
 **/
@RestController
@RequestMapping("/orders")
@Tag(name = "销售单模块")
public class OrderController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IOrdersService orderService;

    @Schema(description = "添加销售单")
    @PostMapping("/add")
    @Transactional
    public R<Long> add(@RequestBody @Validated OrderAddRequest request) throws Exception {
        //如果有originId就是合并销售单，否则就是新增销售单
        request.setCreator(request.getOperator());

        //1.根据客户姓名和手机查询出客户id
        CustomerAddModule customerAddModule = new CustomerAddModule();
        BeanUtils.copyProperties(request, customerAddModule);
        //2.添加销售单

        request.setCompleteStatus(CommonConst.OrderPayStatusEnum.PAYING.getCode());
        //生成订单id并更新
        OrderLatestRequest serialNoCreateRequest = new OrderLatestRequest();
        BeanUtils.copyProperties(request, serialNoCreateRequest);
        String serialNo = orderService.getLatestOrderSerialNo(serialNoCreateRequest);
        request.setSerialNo(serialNo);
        Long orderId = EntityUtils.addEntity(orderService.getBaseMapper(), OrdersEntity.class, request);
        //3.添加订单详情
        request.setId(orderId);
        orderService.addOrderDetail(request);
        return R.ok(orderId);
    }

    @Schema(description = "查询销售单详情")
    @RequestMapping(value = "/detailById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<OrdersResponse> detailById(@RequestBody @Validated IdRequest request) throws Exception {
        return R.ok(orderService.detailResponseById(request.getId()));
    }

    @Schema(description = "查询销售单详情")
    @RequestMapping(value = "/detailByOutTradeNo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<OrdersResponse> detailByOutTradeNo(@RequestBody @Validated OrdersEntity request) throws Exception {
        return R.ok(orderService.detailResponseByOutTradeNo(request.getOutTradeNo()));
    }

    @Schema(description = "查询销售单列表")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public TableDataInfo<OrdersResponse> pageList(@RequestBody @Validated OrderPageListRequest request) throws Exception {
        return orderService.orderPageList(request);
    }


    @Schema(description = "删除销售单")
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<Boolean> delete(@RequestBody @Validated IdRequest request) throws Exception {
        return R.ok(orderService.removeById(request.getId()));
    }


    @Schema(description = "作废销售单")
    @RequestMapping(value = "/invalid", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<Boolean> invalid(@RequestBody @Validated OrderInvalidRequest request) throws Exception {
        request.setUpdater(request.getOperator());
        //先回退库存和金额
        boolean update = orderService.update(new LambdaUpdateWrapper<OrdersEntity>().eq(OrdersEntity::getId, request.getId())
                .set(OrdersEntity::getOrderStatus, request.getOrderStatus())
                .set(OrdersEntity::getUpdater, request.getOperator())
        );
        return R.ok(update);
    }


    @Schema(description = "修改挂单中的商品")
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R update(@RequestBody @Validated OrderUpdateRequest request) throws Exception {
        request.setUpdater(request.getOperator());
        final OrdersEntity orderEntity = orderService.getById(request.getId());
        if (null != orderEntity && null != request.getGoodsList()) {

        }
        return R.ok();
    }


    @Schema(description = "发起支付")
    @RequestMapping(value = "/unifiedOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R<CreateOrderResponse> unifiedOrder(@RequestBody @Validated OrderCompleteRequest request) throws Exception {
        request.setUpdater(request.getOperator());
        return R.ok(orderService.startPayOrder(request));
    }


}
